
# Estimate our main model & define the weight matrix within Province.
# remove everything
rm(list=ls())

# =================================================================================
# ---------------------------------------------------------------------
# Drop outliers in dataset "Data" according to the "v"th column
# at each percentage "p" at bottom and top. The default value of
# p is 0.005. v is a vector of numbers.
# ---------------------------------------------------------------------

outlier <- function(Data, v, p=0.005) {
  out <- c()
  for(i in v) {
    out <- rbind(out, quantile(Data[,i], probs=c(p, 1-p)))
  }
  j <- 1
  for(i in v) {
    Data <- Data[Data[,i]>out[j,1] & Data[,i]<out[j,2], ]; print(dim(Data))
    j <- j+1
  }
  return(Data)
}

# =================================================================================

# Load packages
library(foreign)
library(gmm)
library(plm)

Data <- read.dta('Ind39.dta')

Data <- outlier(Data, which(names(Data)=="sm"), p=0.005)

# Delete obs according to variable "Foreign"
Data <- subset(Data, F1>=0 & F1<=1 & !is.na(Province) & sm<=1 & !is.na(k2))

# =============================================
# Step One
h.lnbe <- mean(log(Data$sm)); h.eta <- - log(Data$sm) + h.lnbe; h.e <- mean(exp(h.eta))
h.betam <- exp(h.lnbe)/h.e

# =============================================
# Step Two
# Generate weights s1 and s2 & variables related to weights

# Generate year dummies
TD <- model.matrix(~as.factor(Data$Year)-1)
TD1 <- model.matrix(~as.factor(Data$Year-1)-1)[,-1]
TD2 <- model.matrix(~as.factor(Data$Year-2)-1)[,-(1:2)]

objfn <- function(beta) {
  
  # Define function "phi" which is the w1
  phi <- function(beta) {
    (1-h.betam)*Data$m1 - h.lnbe - log(Data$ppi1/Data$ppii1) - beta[1]*Data$k1 - beta[2]*Data$l1 - TD1%*%beta[3:9]
  }
  
  phi2 <- function(beta) {
    (1-h.betam)*Data$m2 - h.lnbe - log(Data$ppi2/Data$ppii2) - beta[1]*Data$k2 - beta[2]*Data$l2 - TD2%*%beta[3:8]
  }
  
  # lagged firms' own productivity
  w2 <- phi2(beta)
  
  # Generate weights & variables related to weights
  for(i in 1:nrow(Data)) {
    s1.t <- (w2 > w2[i])*(Data$F1 == 0)*(Data$Year==Data$Year[i])*(Data$Province==Data$Province[i]); s1.t[i] <- 0
    s1.b <- sum(s1.t)
    s1 <- if(s1.b>0) s1.t/s1.b else rep(0, length(s1.t));
    Data$w1k1[i] <- sum(s1*Data$k1); Data$w1m1[i] <- sum(s1*Data$m1, na.rm = TRUE); Data$w1l1[i] <- sum(s1*Data$l1, na.rm = TRUE)
  }
  
  # Define functions: "wphi" and "phi"
  w1phi <- function(beta) {
    (1-h.betam)*Data$w1m1 - h.lnbe - log(Data$ppi1/Data$ppii1) - beta[1]*Data$w1k1 - beta[2]*Data$w1l1 - TD1%*%beta[3:9]
  }
  
  # SSR
  y.star <- Data$y - h.betam*Data$m - beta[1]*Data$k - beta[2]*Data$l - TD%*%beta[3:10]
  p1 <- as.vector(phi(beta)); p2 <- as.vector(w1phi(beta));
  var.poly <- poly(cbind(p1, Data$F1, p2), degree = 2, raw = TRUE)
  mod1 <- lm(as.vector(y.star)~var.poly)
  OBJ <- sum(mod1$residuals^2)
  return(OBJ)
  
}

# optimization
set.seed(1)
initial <- c(0.05, 0.1, 0.05, 0,0,0,0,0,0,0,0)
target <- optim(initial, objfn, method = "Nelder-Mead")

# results - elas
h.betak <- target$par[1]; h.betal <- target$par[2]

# With optimal beta, generate other variables
# lagged firms' own productivity
w1 <- (1-h.betam)*Data$m1 - h.lnbe - log(Data$ppi1/Data$ppii1) - h.betak*Data$k1 - h.betal*Data$l1 - TD1%*%target$par[3:9]
w2 <- (1-h.betam)*Data$m2 - h.lnbe - log(Data$ppi2/Data$ppii2) - h.betak*Data$k2 - h.betal*Data$l2 - TD2%*%target$par[3:8]

# Generate weights & variables related to weights
for(i in 1:nrow(Data)) {
  s1.t <- (w2 > w2[i])*(Data$F1 == 0)*(Data$Year==Data$Year[i])*(Data$Province==Data$Province[i]); s1.t[i] <- 0
  s1.b <- sum(s1.t)
  s1 <- if(s1.b>0) s1.t/s1.b else rep(0, length(s1.t));
  Data$w1k1[i] <- sum(s1*Data$k1); Data$w1m1[i] <- sum(s1*Data$m1, na.rm = TRUE); Data$w1l1[i] <- sum(s1*Data$l1, na.rm = TRUE)
}
# weighted average of other firms' productivity
s1w1 <- (1-h.betam)*Data$w1m1 - h.lnbe - log(Data$ppi1/Data$ppii1) - h.betak*Data$w1k1 - h.betal*Data$w1l1 - TD1%*%target$par[3:9]

# Calculate partial effects
G.coef <- function(beta){
  y.star <- Data$y - h.betam*Data$m - beta[1]*Data$k - beta[2]*Data$l - TD%*%beta[3:10]
  p1 <- as.vector(w1); p2 <- as.vector(s1w1)
  var.poly <- poly(cbind(p1, Data$F1, p2), degree = 2, raw = TRUE)
  mod1 <- lm(as.vector(y.star)~var.poly)
  return(mod1$coef)
}

# results - partial effects
coef <- G.coef(target$par)[2:10]

p.w  <- coef[1] + 2*coef[2]*w1 + coef[4]*Data$F1 + coef[7]*s1w1
p.F  <- coef[3] + coef[4]*w1 + 2*coef[5]*Data$F1 + coef[8]*s1w1
p.sw <- coef[6] + coef[7]*w1 + coef[8]*Data$F1 + 2*coef[9]*s1w1

# The estimated w and w1
Data$w <- Data$y - h.betak*Data$k - h.betal*Data$l - h.betam*Data$m - TD%*%target$par[3:10] - h.eta
Data$w1 <- w1; Data$w2 <- w2; Data$sw1 <- s1w1; Data$eta <- h.eta

# save data
Data$pF <- p.F; Data$pw <- p.w; Data$psw <- p.sw; 
Data39 <- Data[ , c("LPCode", "Firm", "Year","Province","F1","F2",
                    "w1k1", "w1m1", "w1l1", 
                    "w", "w1", "w2", "sw1","pF","pw","psw","eta")]
write.dta(Data39, file = "Ind39_s_Dt_asym_Est.dta")

